package com.example.my_isc_nirscan.adapter;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

import com.example.my_isc_nirscan.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeviceListAdapter extends BaseAdapter {
    private Context context;
    private Map<BluetoothDevice, Integer> deviceRssiMap;
    private List<BluetoothDevice> deviceList;

    public DeviceListAdapter(Context context, Map<BluetoothDevice, Integer> deviceRssiMap) {
        this.context = context;
        this.deviceRssiMap = deviceRssiMap;
        this.deviceList = new ArrayList<>(deviceRssiMap.keySet());
    }

    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public BluetoothDevice getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.bluetooth_device_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = convertView.findViewById(R.id.tv_device_name);
            viewHolder.tvAddress = convertView.findViewById(R.id.tv_device_address);
            viewHolder.tvState = convertView.findViewById(R.id.tv_device_status);
            viewHolder.tvRssi = convertView.findViewById(R.id.tv_device_rssi);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        BluetoothDevice device = getItem(position);
        int rssi = deviceRssiMap.get(device);

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            // 权限未授予，设置默认文本或禁用视图
            System.err.println("getView: " + device.getName() + " " + rssi);
            viewHolder.tvName.setText("Permission not granted");
            viewHolder.tvAddress.setText("");
            viewHolder.tvState.setText("State: " + device.getBondState());
            viewHolder.tvRssi.setText("");
        } else {
            viewHolder.tvName.setText(device.getName());
            viewHolder.tvAddress.setText(device.getAddress());
            viewHolder.tvState.setText("State: " + device.getBondState());
            viewHolder.tvRssi.setText("RSSI: " + rssi);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView tvName;
        TextView tvAddress;
        TextView tvState;
        TextView tvRssi;
    }
}
